<template>
	<view class="u-page2">
		<u-gap :height="vxbottom+5"></u-gap>
		<u-navbar title="上传订单验证信息" @leftClick="rt()"></u-navbar>

		<u--form labelPosition="left" :model="model1" :rules="rules" ref="form1">
			<u-form-item label="美团用户名" prop="orderInfo.name" borderBottom ref="item1">
				<u--input v-model="model1.orderInfo.name" border="none" laceholder="请填写用户名"></u--input>
			</u-form-item>
			<u-form-item label="订单号" prop="orderInfo.ordernumber" borderBottom ref="item2">
				<u--input v-model="model1.orderInfo.ordernumber" border="none" laceholder="请填写订单号"></u--input>
			</u-form-item>
			<u-form-item label="订单金额" prop="orderInfo.money" borderBottom ref="item3">
				<u--input v-model="model1.orderInfo.money" border="none" laceholder="请填写订单金额"></u--input>
			</u-form-item>
			<u-form-item label="订单凭证" borderBottom ref="item4">
				<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
					:maxCount="3"></u-upload>
			</u-form-item>
		</u--form>
		<u-modal :show="show" :content="content" @confirm="qr()" ></u-modal>
		<u-toast ref="uToast" />
		<u-button @click="submit">提交</u-button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				vxbottom: 10,
				show: false,
				content: '',
				id: 0,
				fileList1: [],
				model1: {
					orderInfo: {
						name: 'coffee与小巷',
						ordernumber: '',
						money: '20',
						img1: '',
						img2: '',
						img3: ''
					}
				},
				rules: {
					'orderInfo.name': {
						type: 'string',
						required: true,
						message: '请填写用户名',
						trigger: ['blur', 'change']
					},
					'orderInfo.ordernumber': {
						type: 'string',
						required: true,
						message: '请填写订单号',
						trigger: ['blur', 'change']
					},
					'orderInfo.money': {
						type: 'string',
						required: true,
						message: '请填写订单金额',
						trigger: ['blur', 'change']
					}
				}
			}
		},
		onLoad: function(option) {
			this.id = option.id
		},
		created() {
			// #ifdef MP-WEIXIN
			let menuButtonInfo = uni.getMenuButtonBoundingClientRect()
			this.vxbottom = menuButtonInfo.bottom
			// #endif
		},
		methods: {
			submit() {
				var that = this
				this.$refs.form1.validate().then(res => {
					if (that.model1.orderInfo.img1 == '') {
						that.content = '请上传带有店铺名称和实付金融的图片'
						that.show = true
						return
					} else if (that.model1.orderInfo.img2 == '') {
						that.content = '请上传带有订单号和下单时间的图片'
						that.show = true
						return
					}
					let t = that.model1.orderInfo
					that.$http.postf('orderverify/submitmessage', {
						name: t.name,
						ordernumber: t.ordernumber,
						money: t.money,
						img1: t[`img1`],
						img2: t[`img2`],
						img3: t[`img3`],
						orderid: that.id
					}).then(res => {
						if (res.statusCode == 200) {
							let x =  JSON.parse(res.data).meta
							if (x.code == 200) {
								uni.showToast({
									title: '提交成功',
									duration: 2000,
									success: function() {
										setTimeout(function() {
											uni.navigateBack()
										}, 2000);
									}
								})

							} else {
								that.content = '提交失败'
								that.show = true
								return
							}
						}
					})

				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
			},
			qr() {
				this.show = false
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
				// this.model1.orderInfo[`img${event.name}`] = ''
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			rt() {
				uni.navigateBack()
			},
			uploadFilePromise(url) {
				var that = this
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://localhost:8080/thunder/orderverify/upverifyimg', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							if (res.statusCode == 500) {
								that.content = "上传失败！可能是文件过大"
								that.show = true
							} else {
								let x =  JSON.parse(res.data)
								if (x.meta.code == 400) {
									that.content = "上传失败！服务器错误"
									that.show = true
								} else {
									let x =  JSON.parse(res.data)
									// that.content = "上传成功！"
									that.model1.orderInfo[`img${that.fileList1.length}`] = x.data
									// that.show = true
									setTimeout(() => {
										resolve(res.data.data)
									}, 1000)
								}
							}

						}
					});
				})
			},
		}
	}
</script>

<style>

</style>
